package com.shenhao.hpfa.autoconfigure;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenhao.hpfa.netty.HpfaNettyClient;
import com.shenhao.hpfa.properties.HpfaProperties;
import com.shenhao.hpfa.service.HpfaDeviceService;
import com.shenhao.hpfa.service.HpfaDeviceServiceImpl;
import com.shenhao.hpfa.service.NoopHpfaDeviceService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.shenhao.hpfa")
@ConditionalOnClass(HpfaNettyClient.class)
@EnableConfigurationProperties(HpfaProperties.class)
public class HpfaAutoConfiguration {

    @Bean
    @ConditionalOnProperty(value = "hpfa.device.enabled", havingValue = "true")
    public HpfaDeviceService hpfaDeviceService(HpfaProperties properties, HpfaNettyClient hpfaNettyClient) {
        return new HpfaDeviceServiceImpl(hpfaNettyClient, properties);
    }

    @Bean
    @ConditionalOnProperty(value = "hpfa.device.enabled", havingValue = "false", matchIfMissing = true)
    public HpfaDeviceService noopHpfaDeviceService() {
        return new NoopHpfaDeviceService();
    }

}